package com.scs.application.modules.device.controller;

import cn.hutool.core.bean.BeanUtil;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.modules.device.entity.DeviceLedger;
import com.scs.application.modules.device.entity.DevicePlanItem;
import com.scs.application.modules.device.service.DeviceLedgerService;
import com.scs.application.modules.device.service.DevicePlanItemService;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 *  设备计划明细服务层
 */
@RestController
@RequestMapping("/device/plan_item")
public class DevicePlanItemController extends CrudController<DevicePlanItemService, DevicePlanItem>{
    @Autowired
    private DeviceLedgerService deviceLedgerService;
    @PostMapping("/addByScan")
    synchronized public RestResult addByScan(@RequestBody DevicePlanItem workItem) {
        DeviceLedger ledger = deviceLedgerService.getByDeviceSn(workItem.getDeviceSn());
        BeanUtil.copyProperties(ledger,workItem);
        workItem.setLedgerId(ledger.getId()).setId(null);
        service.saveOrUpdateBatch(Arrays.asList(workItem));
        return RestResult.ok("操作成功");
    }
}